﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sys
{
    /// <summary>
    /// 处理也页面的初始化工作和为其他页面提供基础行操作
    /// </summary>
    public abstract class ClientPage : System.Web.UI.Page
    {


        protected string _Action = string.Empty;

        private Sys.Security.TKIdentity _user;
        /// <summary>
        /// 当前登陆用户的信息
        /// </summary>
        protected Sys.Security.TKIdentity User
        {
            get { return _user; }
        }
        /// <summary>
        /// 页面的初始化工作。
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (System.Web.HttpContext.Current.Session.Contents["AppUser"] != null)
            {
                _user = (Sys.Security.TKIdentity)System.Web.HttpContext.Current.Session.Contents["AppUser"];
                Csla.ApplicationContext.User = new Sys.Security.Principal(_user);
            }
            else { 
                ///准备抛出错误
            }
        }
        /// <summary>
        /// 页面初始化后检查页面权限
        /// </summary>
        /// <param name="e"></param>
        protected override void OnCommitTransaction(EventArgs e)
        {
            base.OnCommitTransaction(e);
        }
        /// <summary>
        /// 登陆系统
        /// </summary>
        /// <param name="name"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool Login(string name, string password)
        {
            Boolean returnBool = false;
            Object returnObject = Sys.Security.Principal.Login(name,password);//Sys.MembershipUser.Login(name, password);

            if (returnObject != null)
            {
                //_user = (MembershipUser)returnObject;
                //Principal= Csla.ApplicationContext.User;
                returnBool = true;
                System.Web.HttpContext.Current.Session.Contents["AppUser"] = returnObject;
            }

            return returnBool;
        }
        /// <summary>
        /// 将服务器执行的结果输出到客户端
        /// </summary>
        /// <returns></returns>
        public void ResponseResult(Boolean Flag,string msg)
        {
            Sys.Web.ClientMessage<string> cm = new Sys.Web.ClientMessage<string>();
            cm.Flag = Flag;
            cm.Message = msg;
            cm.Export();
        }
        /// <summary>
        /// 将翻页信息输出到客户端
        /// </summary>
        /// <param name="Flag"></param>
        /// <param name="msg"></param>
        public void ResponseTurnPage(Boolean Flag, string msg)
        { 
        
        }
      
    }
}
